import { Dialog, Toast } from 'vant'

/**
 * 提示与加载工具类
 */
export default class Tips {
  constructor () {
    this.isLoading = false
  }

  static loading (title = '加载中...') {
    if (Tips.isLoading) {
      return
    }
    Tips.isLoading = true
    Toast.loading({
      duration: 0,
      message: title
    })
  }

  static loaded () {
    if (Tips.isLoading) {
      Tips.isLoading = false
      Toast.clear()
    }
  }

  static toast (message, { type = 'text', duration = 2000 } = {}) {
    Toast({
      message,
      type,
      duration
    })
  }

  static alert ({ title = '', message, confirmButtonText = '确定' } = {}) {
    return new Promise((resolve, reject) => {
      Dialog.alert({
        title,
        message,
        confirmButtonText
      }).then(() => {
        resolve(true)
      })
    })
  }

  static confirm ({ title = '', message, cancelButtonText = '取消', confirmButtonText = '确定' } = {}) {
    return new Promise((resolve, reject) => {
      Dialog.confirm({
        title,
        message,
        confirmButtonText,
        cancelButtonText
      }).then(() => {
        resolve(true)
      }).catch(() => {
        resolve(false)
      })
    })
  }
}
Tips.isLoading = false

